//
//  ArticleDetailController.m
//  easystudy
//
//  Created by kane on 16/8/31.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "ArticleDetailController.h"

@interface ArticleDetailController ()
{
    __weak IBOutlet UIButton *favoriteBtn;
    __weak IBOutlet UIWebView *browser;
    
    __weak IBOutlet UIView *coverView;
    __weak IBOutlet UIButton *shareCancelBtn;
    
    NSMutableArray *collectList;
}

@end

@implementation ArticleDetailController
@synthesize dataInfo;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    collectList = [NSMutableArray array];
    [self reloadArticleInfo];

    NSString *url = [NSString stringWithFormat:@"%@/%@",PageURL,dataInfo[@"artUrl"]];
    NSURLRequest *reqest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [browser loadRequest:reqest];
    [_inputTextView.layer setBorderColor:[UIColor grayColor].CGColor];
    [_inputTextView.layer setCornerRadius:5.0];
    [_inputTextView.layer setBorderWidth:1];
    
    [shareCancelBtn drawCornerRadius:5];
    [shareCancelBtn drawBorderColor:[UIColor lightGrayColor] Width:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadArticleInfo
{
    NSDictionary *param = @{@"type":@"2",@"srcId":self.artID};
    [WebService findFavoriteList:param WithBlock:^(id result, NSError *error) {
        if (!error) {
            [collectList removeAllObjects];
            [collectList addObjectsFromArray:result[@"data"][@"collectlist"]];
            favoriteBtn.selected = collectList.count > 0;
        }
    }];
}

- (IBAction)backBtnPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)favoriteBtnPressed:(UIButton *)sender {
    if (![UserEntity sharedUser].loginSuccess) {
        [self.view makeToast:@"请先登录~"];
        return;
    }
    sender.selected = !sender.selected;
    if (sender.selected) {
        NSDictionary *params = @{@"srcId":self.artID,@"type":@"2"};
        [WebService saveFavorite:params WithBlock:^(id result, NSError *error) {
            if (!error) {
                if ([result[@"data"][@"code"] boolValue]) {
                    [self.view makeToast:@"收藏成功"];
                }else{
                    [self.view makeToast:@"收藏失败"];
                    sender.selected = !sender.selected;
                }
            }
        }];
    }else{
        for (NSDictionary *dic in collectList) {
            NSDictionary *params = @{@"cltid":dic[@"cltid"]};
            [WebService deleteFavorite:params WithBlock:^(id result, NSError *error) {
                if (!error) {
                    if ([result[@"data"][@"code"] boolValue]) {
                        [self.view makeToast:@"删除成功"];
                    }else{
                        [self.view makeToast:@"删除失败"];
                        sender.selected = !sender.selected;
                    }
                }
            }];
        }
    }
}

- (IBAction)shareBtnPressed:(id)sender
{
    coverView.hidden = NO;
}

- (IBAction)coverBtnPressed:(id)sender
{
    coverView.hidden = YES;
}

- (IBAction)cancelShare:(id)sender
{
    coverView.hidden = YES;
}

- (IBAction)emojiPanel:(id)sender {
    [_inputTextView resignFirstResponder];

    if (![UserEntity sharedUser].loginSuccess) {
        [self.view makeToast:@"请先登录"];
        return;
    }
    if (_inputTextView.text.length == 0) {
        [self.view makeToast:@"评论要先说点什么哦~"];
        return;
    }
    NSString *cstid = [UserEntity sharedUser].UserID;
    if (cstid.length == 0) {
        cstid = @"";
    }
    if ([UserEntity sharedUser].loginSuccess) {
        NSDictionary *params = @{@"artId":self.artID,@"cstid":cstid,@"comment":_inputTextView.text};
        [WebService saveArticleComment:params WithBlock:^(id result, NSError *error) {
            if (!error) {
                BOOL success = [result[@"data"][@"code"] boolValue];
                [self.view makeToast:success ? @"评论成功,等待审核~":@"评论失败"];
                if (success) {
                    _inputTextView.text = @"";
                }
            }
        }];
    }
}

#pragma mark - TextViewDelegate
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    //监听键盘弹出
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
    return YES;
}

-(void)keyboardWillAppear:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    //取出动画时长
    CGFloat animationDuration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    //取出键盘位置大小信息
    CGRect keyboardBounds = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //rect转换
    //    CGRect keyboardRect = [self.view convertRect:keyboardBounds toView:nil];
    //记录Y轴变化
    CGFloat keyboardHeight = keyboardBounds.size.height;
    //上移动画options
    UIViewAnimationOptions options = (UIViewAnimationOptions)[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        _chatView.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);
    } completion:nil];
}

-(void)keyboardWillDisappear:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    //取出动画时长
    CGFloat animationDuration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    //下移动画options
    UIViewAnimationOptions options = (UIViewAnimationOptions)[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
    //回复动画
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        _chatView.transform = CGAffineTransformIdentity;
    } completion:nil];
}

@end
